package com.yss.reportworld.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yss.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yss.reportworld.service.IDataReportService;
import com.yss.reportworld.mapper.DataReportMapper;
import com.yss.reportworld.domain.DataReport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 基础数据对应报表信息Service业务层处理
 *
 * @author yss
 * @date 2023-03-24
 */
@Slf4j
@Service
public class DataReportServiceImpl extends ServiceImpl<DataReportMapper, DataReport> implements IDataReportService {

    /**
     * 查询对应数据和报表对应关系
     *
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, List<String>> getDataReportMap() {
        QueryWrapper<DataReport> wrapper = new QueryWrapper<>();
        // 所属报表 code和数据，对应一个list
        List<DataReport> reportList = this.list(wrapper);
        if (StringUtils.isEmpty(reportList)) {
            return null;
        }
        Map<String, List<String>> mapDataReport = new HashMap<>();
        for (DataReport report : reportList) {
            if (null == report || StringUtils.isEmpty(report.getDataId()) || StringUtils.isEmpty(report.getDataCode())) {
                continue;
            }
            String key = report.getDataCode() + "\t" + report.getDataId();
            if (mapDataReport.containsKey(key)) {
                List<String> list = mapDataReport.get(key);
                list.add(report.getReportId());
                mapDataReport.put(key, list);
            } else {
                List<String> list = new ArrayList<>();
                list.add(report.getReportId());
                mapDataReport.put(key, list);
            }
        }
        return mapDataReport;
    }

}
